package yl.hs.bmipfull.service.construction.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.production.IProductShipOrderDao;
import yl.hs.bmipfull.service.construction.IConstructionDashboardService;
import yl.hs.bmipfull.service.salemanage.ISaleContractService;
import yl.hs.bmipfull.service.salemanage.impl.SaleBaseService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 韩纪旺
 * @apiNote
 * @since 2022/11/21 15:05 创建
 */
@Service
public class ConstructionDashboardServiceImpl extends SaleBaseService implements IConstructionDashboardService {



    @Autowired
    ISaleContractService contractService;
    @Autowired
    IProductShipOrderDao shipOrderDao;




    @Override
    public Map<String, Object> dataIndicators()   {
        Map<String,Object> map =new HashMap<>();
        List<String> snList = null;
        PageResult<Map<String, Object>> result = null;
        try {
            snList = getConstructionContractSN();
            //合同数量
            result = contractService.queryCustomerContracts(new PageQuery(),10,1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //发货单数量
        var quantity = shipOrderDao.quantity(snList);
        //累计发货量
        var cumulative = shipOrderDao.cumulative(snList);
        //累计发货金额
        var cumulativeMoney = shipOrderDao.cumulativeMoney(snList);
        //已结算发货量
        var settled = shipOrderDao.settled(snList);
        //已结算的发货金额
        var bigDecimal = shipOrderDao.settledMoney(snList);
        //已签收对账单
        var bigDecimal1 = shipOrderDao.signedIn(snList);
        //待签收对账单
        var unsigned = shipOrderDao.unsigned(snList);
        //账户金额
        var money = shipOrderDao.money(snList);
        //账户余额
        var balance = shipOrderDao.balance(snList);
        map.put("合同数量",result.getTotal());map.put("发货单数量",quantity);map.put("累计发货量",cumulative);map.put("累计发货金额",cumulativeMoney);
        map.put("已结算发货量",settled);
        map.put("已结算的发货金额",bigDecimal);
        map.put("已签收对账单",bigDecimal1);
        map.put("待签收对账单",unsigned);
        map.put("账户金额",money);
        map.put("账户余额",balance);

        return map;
    }

    @Override
    public Map<String, Object> weightMoneyMonthly() {
        //返回值设值
        Map<String, Object> result = new HashMap<>();
        List<String> snList = null;

        try {
            snList = getConstructionContractSN();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //今年1月，将cNow对象时间设置为本年一月份
        Calendar cNow = Calendar.getInstance();
        cNow.setTime(new Date());
        cNow.clear(Calendar.MONTH);

        //月份集合
        List<String> monthStr = new ArrayList<>();
        //本期每月重量
        List<BigDecimal> nowNetWeight = new ArrayList<>();
        List<BigDecimal> money = new ArrayList<>();
        //循环查询1-12月销售重量（本期与上期）
        for (int i = 0; i < 12; i++) {
            if (i > 0) {
                cNow.add(Calendar.MONTH, 1);
            }
            monthStr.add((i + 1) + "月");
            var weightMonthly = shipOrderDao.weightMonthly(snList, cNow.getTime());
            var moneyMonthly = shipOrderDao.ReviewedMoneyMonthly(snList, cNow.getTime());
            var bigDecimal = shipOrderDao.unapprovedMoneyMonthly(snList, cNow.getTime());
            nowNetWeight.add(weightMonthly);
            money.add(moneyMonthly.add(bigDecimal));

        }
        result.put("Monthly",monthStr);
        result.put("weight",nowNetWeight);
        result.put("money",money);
        return result;


    }
}
